import {Component} from '@angular/core';
import {NavController, ModalController, AlertController, Platform} from 'ionic-angular';

import {LoginModelPage} from '../login/login';
import {AboutPage} from './about/about';
import {TagsInputPage} from './tagsInput/tagsInput';
import {ContactPage} from '../../component/contact-component/contact.component';
import {UserInfo} from '../../model/userInfo';

import {GoodsPage} from './goods/goods';
import {SpecialPage} from './special/special';
import {HttpService} from "../../providers/HttpService";

@Component({
    selector: 'page-mine',
    templateUrl: 'mine.html'
})
export class MinePage {

    goodsPage = GoodsPage;
    specialPage = SpecialPage;

    userinfo:UserInfo = new UserInfo;
    constructor(public navCtrl:NavController,
                private modalCtrl:ModalController,
                private httpService:HttpService,
                private alertCtrl:AlertController) {
    }


    ionViewWillEnter(){
    }

    ionViewDidEnter(){
    }

    login() {
        let modal = this.modalCtrl.create(LoginModelPage);
        modal.present();
        modal.onDidDismiss(result=>{
            this.userinfo = result;
        })
    }

    logout(){
        let alert = this.alertCtrl.create({
            title: '退出登录',
            cssClass:'logout-alert',
            buttons: [
                {
                    text: '取消',
                    cssClass:'logout-cancel'
                },
                {
                    text: '确定',
                    cssClass:'logout-ok',
                    handler: () => {
                       this.userinfo = new UserInfo;
                    }
                }
            ]
        });

        alert.present();

    }

    about(){
        DrisBridge.getLocation();
        // this.navCtrl.push(AboutPage);
    }

    learn(){
        this.navCtrl.push(ContactPage)
    }

    startSpeech(){
        console.log('开启语音识别');
        cordova.plugins.bdasr.startSpeechRecognize();
        let that = this;
        var speechResult = [];
        cordova.plugins.bdasr.addEventListener(res => {

            console.log('语音识别监听中');
            // res参数都带有一个type
            if (!res) {return;}
            switch (res.type) {
                case "asrReady": {
                    // 识别工作开始，开始采集及处理数据
                    console.log('识别工作开始，开始采集及处理数据');
                    break;
                }
                case "asrBegin": {
                    // 检测到用户开始说话
                    console.log('检测到用户开始说话');
                    break;
                }
                case "asrEnd": {
                    // 本地声音采集结束，等待识别结果返回并结束录音
                    console.log('本地声音采集结束，等待识别结果返回并结束录音');
                    // cordova.plugins.bdasr.closeSpeechRecognize();
                    break;
                }
                case "asrText": {
                    // 语音识别结果
                    console.log('语音识别结果');
                    console.log(res);
                    speechResult.push(res);
                    break;
                }
                case "asrFinish": {
                    // 语音识别功能完成
                    console.log('语音识别功能完成');
                    console.log('----------------------------');
                    console.log(speechResult[speechResult.length-1]);
                    console.log('----------------------------');
                    console.log(speechResult);
                    console.log('----------------------------');
                    let finishRes = speechResult[speechResult.length-1];
                    let title = finishRes.message.results_recognition[0];
                    this.aiForTitle(title);
                    speechResult.length = 0;
                    break;
                }
                case "asrCancel": {
                    // 语音识别取消
                    console.log('语音识别取消');
                    break;
                }
                default:
                    break;
            }
        }, error => {
            console.log('语音识别错误');
            console.log(error);
            this.startSpeech()
        });
    }

    stopSpeech(){
        console.log('关闭语音');
        // 主动结束语音识别
        // cordova.plugins.bdasr.cancelSpeechRecognize();
        this.aiForTitle('我加个班，从晚上6点到9点')
    }

    aiForTitle(title:string) {
        console.log(title);

        this.httpService.post('http://192.168.1.164:8080/HubotService/ai/show',{
            message:title,
            userid:2,
            sceneid:17699
        }).subscribe(res => {
            alert(JSON.stringify(res));
            // this.startSpeech();
        },error => {
            console.log(JSON.stringify(error));
            // this.startSpeech();
        })
    }

    tagsInputPage(){
        this.navCtrl.push(TagsInputPage);
    }
}
